// vite.config.ts
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
// import commonjsExternals from 'vite-plugin-commonjs-externals';

export default defineConfig({
  plugins: [
    vue(),
    // commonjsExternals({
		//   externals: ['node:path','node:fs/promises'] // 这里
		// })
  ],
  build: {
    // 库模式
    lib: {
      entry: [
        "./src/utils/index.ts",
        "./src/utils/useBase64.ts",
        "./src/utils/useFile.ts",
        "./src/utils/useCache.ts",
        "./src/utils/useDrag.ts",
        "./src/utils/useLazy.ts",
        "./src/utils/useResize.ts",
        "./src/utils/useScroll.ts",
        "./src/plugins/vite-plugin-service-worker.ts",
      ],
      name: "vue-cache-optimization",
      fileName: "vue-cache-optimization",
    },
    sourcemap: false,
    rollupOptions: {
      external: ["vue", "jsqr", "qrcode"],
      plugins: [resolve(), commonjs()],
      output: [
        {
          globals: {
            vue: "Vue",
          },
          // preserveModules: true, // 让打包目录和我们目录对应
          format: "es",
          entryFileNames: "[name].mjs",
          chunkFileNames: "[name].mjs",
          assetFileNames: "assets/[name].[ext]",
          dir: "dist",
          exports: "named",
        },
        {
          globals: {
            vue: "Vue",
          },
          format: "cjs",
          entryFileNames: "[name].js",
          chunkFileNames: "[name].js",
          assetFileNames: "assets/[name].[ext]",
          dir: "dist",
          exports: "named",
        },
      ],
    },
  },
  server: {
    proxy: {
      '/api': {
        target: 'http://localhost:3000',
        rewrite: (path) => path.replace(/^\/api/, ''),
      }
    }
  }
});
